home *** CD-ROM | disk | FTP | other *** search
- Path: engnews1.Eng.Sun.COM!taumet!clamage
- From: clamage@Eng.sun.com (Steve Clamage)
- Newsgroups: comp.std.c++
- Subject: Re: "explicit" default constructor?
- Date: 28 Mar 1996 16:20:38 GMT
- Organization: Sun Microsystems Inc.
- Approved: clamage@eng.sun.com (comp.std.c++)
- Message-ID: <4jeds0$h0b@engnews1.Eng.Sun.COM>
- References: <315A5C7A.6AF5@cs.tu-berlin.de>
- Reply-To: clamage@Eng.sun.com
- NNTP-Posting-Host: taumet.eng.sun.com
- X-Nntp-Posting-Host: taumet.eng.sun.com
- Content-Length: 2504
- X-Lines: 79
- Originator: clamage@taumet
-
- In article 6AF5@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de> writes:
- >Steve Clamage wrote:
- >
- >> A converting constructor is one which can be called with a single parameter.
- >> The default constructor isn't a converting constructor except in the
- >> case of a constructor with all parameters having default values.
- >>
- >> If a constructor has no parameters, declaring it "explicit" has no effect,
- >> since it can never be invoked for a conversion.
- >
- >> If a constructor's parameters all have default values, it can be a
- >> converting constructor, and declaring it "explicit" prevents it from
- >> being invoked implicitly.
- >
- >Yes, I've had this in mind, too. In the following case:
- >
- >class A
- >{
- > explicit A( int x=0 );
- >};
- >
- >how do you call the default constructor?
-
- The same way you always did before. The default constructor is one which
- requires no parameters. If implicitly or explicitly invoked with no
- parameters, no conversion is taking place, and it is a non-converting
- constructor.
-
- > Can it still be invoked implicitly
- >and what is the "explicit" syntax if not? IMO, something like
- >
- >A a;
- >
- >is not explicit
-
- I don't think it matters whether you consider this explicit or not.
- (I would say it is implicit.) No type conversion is taking place, so
- whether the constructor is declared "explicit" or not doesn't matter.
-
- > An explicit call would rather be
- >
- >A a();
-
- No, that declares 'a' to be a function with no parameters returning an
- 'A' by value. It is an unfortunate consequence of C declaration syntax
- colliding with C++ intialization syntax.
-
- You can invoke the default constructor explicitly to create an anonymous
- temporary object in the same way as before, whether or not you supply
- parameters, and whether or not it is declared explicit. Examples:
-
- class C {
- public:
- C(int=0);
- };
-
- foo( const C& );
-
- foo( C() ); // #1
- foo( C(2) ); // #2
- foo( 3 ); // #3
-
- In #1, C's (default) constructor is explicitly invoked to create a temp
- object to be passed to foo. It doesn't matter if the constructor was
- declared explicit or not in this case.
-
- In #2, C's constructor is explicitly invoked to convert the int value 2 to
- a temp C object to be passed to foo. Again, C::C() can be explicit or not.
-
- In #3, C's constructor is invoked implicitly to convert the int value 3 to
- a temp C object to be passed to foo. But if the constructor were declared
- "explicit", this use would be invalid. You would have to use the form of #2.
-
- ---
- Steve Clamage, stephen.clamage@eng.sun.com
-
-
-
-
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-